#include "glad/glad.h"

#include "OpenGLBuffer.h"

namespace Cogs {

	OpenGLVertexBuffer::OpenGLVertexBuffer(uint32_t size){
		glCreateBuffers(1, &m_RendererID);
		glNamedBufferStorage(m_RendererID, size, nullptr, GL_DYNAMIC_STORAGE_BIT);
	}

	OpenGLVertexBuffer::OpenGLVertexBuffer(float* vertices, uint32_t size){
		glCreateBuffers(1, &m_RendererID);
		glNamedBufferStorage(m_RendererID, size, vertices, GL_DYNAMIC_STORAGE_BIT);
	}

	OpenGLVertexBuffer::~OpenGLVertexBuffer() {
		glDeleteBuffers(1, &m_RendererID);
	}

	void OpenGLVertexBuffer::Bind() const {
		glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
	}

	void OpenGLVertexBuffer::UnBind() const {
		glBindBuffer(GL_ARRAY_BUFFER, 0);
	}

	void OpenGLVertexBuffer::SetData(const void* data, uint32_t size) {
		//glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
		glNamedBufferSubData(m_RendererID, 0, size, data);
	}

	const BufferLayout& OpenGLVertexBuffer::GetLayout() const {
		return m_Layout;
	}

	void OpenGLVertexBuffer::SetLayout(const BufferLayout& layout) {
		m_Layout = layout;
	}

	OpenGLIndexBuffer::OpenGLIndexBuffer(uint32_t* indices, uint32_t count)
		:m_Count(count){
		glCreateBuffers(1, &m_RendererID);
		glNamedBufferStorage(m_RendererID, count * sizeof(uint32_t), indices, GL_DYNAMIC_STORAGE_BIT);
	}

	OpenGLIndexBuffer::~OpenGLIndexBuffer(){
		glDeleteBuffers(1, &m_RendererID);
	}

	void OpenGLIndexBuffer::Bind() const{
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
	}

	void OpenGLIndexBuffer::UnBind() const{
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	}
	
	uint32_t OpenGLIndexBuffer::GetCount() const {
		return m_Count;
	}
}